#include "./GameEngine.h"


bool shouldExit = false;
bool isInputListenerExitReady = false;

GameEngine::GameEngine() {}
GameEngine::~GameEngine() {}

// 游戏初始化
void GameEngine::Initialize()
{
    spdlog::debug(">>>>>>>>>> GameEngine init start ");
    setGameState(G_STATE_PREPARE);
    window = new GameWindow();
    inputListener = new InputListener();
    inputListener->startListening();

    // testSquare = new Item(10.0f, 10.0f, "player");
    // testSquare->SetWidth(10);
    // testSquare->SetHeight(10);


    setGameState(G_STATE_RUNNING);
}

void GameEngine::Start()
{
    if (this->IsRunning())
    {
        // 处理用户输入
        this->HandleInput();

        // 更新游戏逻辑
        this->Update();

        // 执行物理模拟
        this->SimulatePhysics();

        // 渲染游戏画面
        this->Render();

        // 处理游戏事件
        this->ProcessEvents();
    }
    else
    {
        spdlog::debug("Start cleanup");
        shouldExit = true;
        // 游戏结束时的清理工作
        // game.Cleanup();
        inputListener->finsh();

        spdlog::debug("cleanup finish");
        while(1){
            if(isInputListenerExitReady)
            {
                break;
            }
        }
        exit(0); 
    }
}

// 游戏主循环
bool GameEngine::IsRunning()
{
    window->GetWindowClosed();
    if(shouldExit)
    {
        spdlog::debug("Game finish!!");
        setGameState(G_STATE_FINISH);
        return false;
    }
    return true;
}

// 处理用户输入
void GameEngine::HandleInput() {}

// 更新游戏逻辑
void GameEngine::Update() {}

// 执行物理模拟
void GameEngine::SimulatePhysics() {}

// 渲染游戏画面
void GameEngine::Render() {
    BeginBatchDraw();





    EndBatchDraw();
}

// 处理游戏事件
void GameEngine::ProcessEvents() {}

// 资源加载和卸载
void GameEngine::LoadResources() {}
void GameEngine::UnloadResources() {}

// 切换游戏状态
// void GameEngine::ChangeGameState(GameState newState){}